﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace AircraftBattleClient
{
    /// <summary>
    /// Interaction logic for AdminLounge.xaml
    /// </summary>
    public partial class AdminLounge : Window
    {
        public string username;

        public struct playerInfo
        {
            public string userName { set; get; }
            public bool blockInfo { set; get; }
        }

        private Thread backgroundThread;
        private static GameEngineServiceClient clt = new GameEngineServiceClient();

        public AdminLounge(string username)
        {
            InitializeComponent();

            this.username = username;
            this.label1.Content = username;

            ((DataGridTextColumn)this.dataGrid1.Columns[0]).Binding = new Binding("userName");
            ((DataGridTextColumn)this.dataGrid1.Columns[1]).Binding = new Binding("blockInfo");


            int max = clt.GetMaxUserID();

            for (int i = 1; i <= max; i++)
            {
                string playerName = clt.GetUserName(i);
                if (playerName != null)
                    this.dataGrid1.Items.Add(new playerInfo() { userName = playerName, blockInfo = clt.GetBlockInfo(playerName) });
            }

            BGWorker3 threadWork = new BGWorker3(this);
            Thread newThread =
                new Thread(new ThreadStart(threadWork.Run));
            this.backgroundThread = newThread;
            this.backgroundThread.Start();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.backgroundThread.Abort();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid1.CurrentItem != null)
            {
                playerInfo p = (playerInfo)this.dataGrid1.CurrentItem;
                clt.SetBlockInfo(p.userName, true, username, clt.GetPassword(username));
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid1.CurrentItem != null)
            {
                playerInfo p = (playerInfo)this.dataGrid1.CurrentItem;
                clt.SetBlockInfo(p.userName, false,username, clt.GetPassword(username));
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            Login log = new Login();
            log.Show();
            this.backgroundThread.Abort();
            this.Close();
        }

    }

    public class BGWorker3
    {
        AdminLounge adminLounge;
        static GameEngineServiceClient clt = new GameEngineServiceClient();

        public BGWorker3(AdminLounge lounge)
        {
            this.adminLounge = lounge;
        }

        public void Run()
        {
            while (true)
            {
                // Background thread can only access the UI thread via
                // the UI thread's dispatcher
                this.adminLounge.Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(() => { UpdateGrid(); }));
                Thread.Sleep(500);
            }
        }

        public void UpdateGrid()
        {

            this.adminLounge.dataGrid1.Items.Clear();

            int max = clt.GetMaxUserID();

            for (int i = 1; i <= max; i++)
            {
                string playerName = clt.GetUserName(i);
                if (playerName != null)
                    this.adminLounge.dataGrid1.Items.Add(new AdminLounge.playerInfo() { userName = playerName, blockInfo = clt.GetBlockInfo(playerName) });
            }

        }
    }
}
